<?php
namespace logic\set\ad;
/**
 * 广告管理
 */
class Ad extends \logic\Base{
    
    
    /**
     * 读取一条记录
     */
    public function find($param){
        return D('Ad','setAd')->find($param);
    }
    
    /**
     * 添加
     */
    public function add($data){
        $data['starttime'] = $data['starttime'] ? strtotime($data['starttime']) : time();
        $data['endtime']  = $data['endtime']  ? strtotime($data['endtime']) : strtotime("+1years");
        $check = [            
            'sort'      => ['def'=> 50,'reg'=>'int'],
            'positionid'=> ['check'=>'must','msg'=> '广告位参数不能为空','reg'=>'int'],
            'pic'       => ['check'=>'must','msg'=> '请上传广告图片'],
            'name'      => '',
            'des'       => '',            
            'url'       => '',
            'starttime' => ['reg'=>'int'],
            'endtime'   => ['reg'=>'int'],
            'siteid'    => ['reg'=>'int']
        ];
        $save = pVerify($check,$data); 
        $adposition = D('Adposition','setAd')->find(['id'=> $data['positionid']]);
        if( !$adposition ) logicE('广告位不存在');
        if( $adposition['status'] != 1 ) logicE('广告位已经关闭');
        $save['tag'] = $adposition['tag'];        
        $id = D('Ad','setAd')->add($save);
        return $id;
    }
    
    /**
     * 编辑
     */
    public function edit($id,$data){
        $data['starttime'] = strtotime($data['starttime']);
        $data['endtime']   =  strtotime($data['endtime']);
        return D('Ad','setAd')->edit($id,$data);
    }
    
    
    /**
     * 修改状态
     */
    public function  editStatus($id,$siteId = ''){
        $info = D('Ad','setAd')->find(['id'=>$id]);
        if( !$info ) return false;
        if( $this->CallUser->role == 'site' ){
            if( $siteId != $info['siteid'] ) logicE('非法操作');
        }
        $data['status'] = $info['status']==1?2:1;
        return D('Ad','setAd')->edit($id,$data);
    }
    /**
     * 获取列表
     */
    public function getAdminList($param){
        return D('Ad','setAd')->getList($param);
    }
    
    /**
     * 站点应用
     */
    public function getWebList($param){
        
    }
    
    
}